<?php namespace Model;

    class adminModel {
        private $DB;
        private $MYSQLI;

        public function __construct() {
            $this->DB = mysqli_connect('kvd_mysql', 'root', 'root', 'kvd');
            $this->MYSQLI = new \mysqli('kvd_mysql', 'root', 'root', 'kvd');
        }

        public function addAdmin($phone, $password, $email = null) {
            $regexp = '/^\s?(\+\s?7|8)([- ()]*\d){10}$/';
            $data = date("Y.m.d");
            $password = md5($password);
            $sql = "";

            if (!(preg_match($regexp, $phone)) && !preg_replace("/[^0-9]/", '', $phone)) {
                return [
                    "status" => 0
                ];
            }
            if ($phone[0] == "8") {
                $phone[0] = "7";
            }
            if ($phone[0] == "+") {
                $phone = str_replace("+", "", $phone);
            }

            $this->DB->real_escape_string($sql);
            $sql = "select * from admin where phone = $phone";
            $response = $this->DB->query($sql)->fetch_assoc();

            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => 0
                ];
            } else {
                $sql = "INSERT INTO admin (phone, email, password, permission ,created, updated)
                            VALUES
                            ('$phone', '$email', '$password',1,'$data', '$data')";
                $this->DB->real_escape_string($sql);
                $this->DB->query($sql);
                $response = $this->DB->query("select * from admin where phone = $phone");
                if ($response != null) {
                    return [
                        "response" => $response->fetch_assoc(),
                        "phone" => $phone,
                        "status" => 1
                    ];
                } else {
                    return [
                        "status" => 0
                    ];
                }
            }
        }

        public function getAdminBy($id = null, $phone = null, $email = null) {
            $sql = "select * from `admin` where ";
            $response = "";

            $obj = [
                "id" => $id,
                "phone" => $phone,
                "email" => $email
            ];

            foreach ($obj as $key => $item) {
                if ($item !== null) {
                    $sql = $sql."$key = '$item'";
                } else {
                    continue;
                }
            }
            $this->DB->real_escape_string($sql);
            $response = $this->DB->query($sql);
            $response = $response->fetch_assoc();
            if ($response === null) {
                return [
                    "status" => 0
                ];
            } else {
                return [
                    "status" => 1,
                    "response" => $response
                ];
            }
        }

        public function getAllAdmin() {
            $sql = "select * from kvd.admin where 1;";
            $sql = $this->DB->real_escape_string($sql);
            $response = $this->DB->query($sql)->fetch_all(MYSQLI_ASSOC);
            if ($response !== null) {
                return [
                    "response" => $response,
                    "status" => 1
                ];
            } else {
                return [
                    "status" => 0
                ];
            }
        }

        public function adminLogin($phone, $password) {
            $response = [];
            $password = md5($password);
            $regexp = '/^\s?(\+\s?7|8)([- ()]*\d){10}$/';

            if (!(preg_match($regexp, $phone)) && !preg_replace("/[^0-9]/", '', $phone)) {
                return [
                    "status" => 0
                ];
            }
            if ($phone[0] == "8") {
                $phone[0] = "7";
            }
            if ($phone[0] == "+") {
                $phone = str_replace("+", "", $phone);
            }

            $sql = "select * from `admin` where phone = '$phone' and password = '$password'";
            $this->DB->real_escape_string($sql);
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response === null) {
                return [
                    "status" => 0,
                ];
            } else {
                return [
                    "response" => $response,
                    "status" => 1,
                ];
            }
        }

        public function checkPermission($id = null) {
            if ($id == null) {
                return [
                    "status" => 0
                ];
            }
            $id = (int) $id;
            $sql = "select * from admin where id = $id";
            $response = $this->DB->query($sql)->fetch_assoc();
            if ($response == null) {
                return [
                    "status" => 0
                ];
            } else {
                return [
                    "status" => 1
                ];
            }
        }

    }